/*
 Fast Artificial Neural Network Library (fann)
 Copyright (C) 2003-2012 Steffen Nissen (sn@leenissen.dk)

 This library is free software; you can redistribute it and/or
 modify it under the terms of the GNU Lesser General Public
 License as published by the Free Software Foundation; either
 version 2.1 of the License, or (at your option) any later version.

 This library is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 Lesser General Public License for more details.

 You should have received a copy of the GNU Lesser General Public
 License along with this library; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <stdio.h>

#include "fann.h"

int main() {
	const unsigned int num_layers = 3;
	const unsigned int num_neurons_hidden = 96; //MSE error on test data: 0.006265
	//const unsigned int num_neurons_hidden = 96*2; //MSE error on test data: 0.008262
	//const unsigned int num_neurons_hidden = 96 * 4;//MSE error on test data: 0.007696
	//const unsigned int num_neurons_hidden = 96 * 8;	//MSE error on test data: 0.008265
	//const unsigned int num_neurons_hidden = 96 * 16;//MSE error on test data: 0.014890
	//const unsigned int num_neurons_hidden = 96 * 32;//MSE error on test data: 0.014890
	const float desired_error = (const float) 0.001;
	struct fann *ann;
	struct fann_train_data *train_data, *test_data;

	unsigned int i = 0;

	printf("Creating network.\n");

	train_data = fann_read_train_from_file("datasets/quadro.train");

	ann = fann_create_standard(num_layers, train_data->num_input,
			num_neurons_hidden, train_data->num_output);

	printf("Training network.\n");

	fann_set_training_algorithm(ann, FANN_TRAIN_RPROP);
	//fann_set_learning_momentum(ann, 0.4f);

	fann_train_on_data(ann, train_data, 300, 10, desired_error);

	printf("Testing network.\n");

	test_data = fann_read_train_from_file("datasets/quadro.test");

	fann_reset_MSE(ann);
	for (i = 0; i < fann_length_train_data(test_data); i++) {
		fann_test(ann, test_data->input[i], test_data->output[i]);
	}
	printf("MSE error on test data: %f\n", fann_get_MSE(ann));

	printf("Saving network.\n");

	fann_save(ann, "robot_float.net");

	printf("Cleaning up.\n");
	fann_destroy_train(train_data);
	fann_destroy_train(test_data);

//	-0.07633 -0.13767 0.92604

	fann_type input[7];
	fann_type *calc_out;

	input[0] = -0.07615;
	input[1] = -0.13836999;
	input[2] = 0.92579997;
	input[3] = 1371;
	input[4] = 1371;
	input[5] = 1371;
	input[6] = 1371;

	calc_out = fann_run(ann, input);

	printf("quadro (%f,%f,%f)\n", calc_out[0], calc_out[1], calc_out[2]);

	fann_destroy(ann);

	return 0;
}
